package com.joysuch.wwyt.datadump.biz.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import com.joysuch.wwyt.datadump.biz.model.EduCertificateUserDumpBean;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * 描述：日期比较
 *
 * @author zhangcancan
 * @date 2023/6/9 15:09
 **/
@Component
public class DateCompareToConvertor implements FieldValueConvert<String, Date> {


    @Override
    public Date convert(Object bean, String value, DataDumpField dataField) {
        if (value == null) {
            return null;
        }

        try {
            Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(value);
            if (bean instanceof EduCertificateUserDumpBean) {
                String takenTime = ((EduCertificateUserDumpBean) bean).getTakenTime();
                if (Objects.nonNull(takenTime)) {
                    Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse(takenTime);
                    if (startDate.compareTo(endDate) > 0) {
                        throw new IllegalArgumentException("日期非法,有效期开始日期不能大于有效期结束日期");
                    }
                }
            }

            return endDate;
        } catch (ParseException e) {
            throw new IllegalArgumentException(value + " 日期格式非法,正确格式为[yyyy-MM-dd]");
        }
    }
}
